/*
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 * 
 * The Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
 *
 * The Initial Developer of the Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
 * Portions created by Mark A. Kobold are Copyright (C) 2000-2007. All Rights Reserved.
 *
 * Contributor(s): 
 *  Mark A. Kobold [mkobold <at> isqlviewer <dot> com].
 *  
 * If you didn't download this code from the following link, you should check
 * if you aren't using an obsolete version: http://www.isqlviewer.com
 */
package org.isqlviewer.sql;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Blob;
import java.sql.SQLException;

import org.isqlviewer.util.BasicUtilities;

/**
 * Utility Implementation for dealing with BLOB objects in memory.
 * <p>
 * This is very easy implementation of a BLOB using the ByteArrayInputStream. This class is designed for easily creating
 * BLOB objects at runtime using a byte array.
 * 
 * @see java.io.ByteArrayInputStream
 * @author Markus A. Kobold &lt;mkobold at sprintpcs dot com&gt;
 */
public class ByteArrayBlob implements Blob {

    protected ByteArrayOutputStream blob = new ByteArrayOutputStream();

    public ByteArrayBlob() {

    }

    public ByteArrayBlob(byte[] data) throws IOException {

        blob.write(data);
    }

    /**
     * @param binaryStream
     */
    public ByteArrayBlob(InputStream binaryStream) throws IOException {

        BasicUtilities.copyStream(binaryStream, blob);
    }

    public long length() {

        return blob.size();
    }

    public byte[] getBytes(long pos, int length) throws SQLException {

        try {
            byte[] copy = new byte[length];
            System.arraycopy(blob.toByteArray(), (int) pos, copy, 0, length);
            return copy;
        } catch (Throwable t) {
            throw new SQLException(t.getMessage());
        }
    }

    public InputStream getBinaryStream() throws SQLException {

        try {
            return new ByteArrayInputStream(blob.toByteArray());
        } catch (Throwable t) {
            throw new SQLException(t.getMessage());
        }
    }

    public long position(byte[] pattern, long start) throws SQLException {

        try {
            String s = new String(pattern);
            String b = blob.toString();
            return b.indexOf(s);
        } catch (Throwable t) {
            throw new SQLException(t.getMessage());
        }
    }

    public long position(Blob pattern, long start) throws SQLException {

        return position(pattern.getBytes(0, (int) pattern.length()), start);
    }

    public void truncate(long length) throws SQLException {

        try {
            int len = (int) length;
            byte[] copy = new byte[len];
            System.arraycopy(blob.toByteArray(), 0, copy, 0, len);
            blob.reset();
            blob.write(copy);
        } catch (Throwable t) {
            throw new SQLException(t.getMessage());
        }
    }

    public OutputStream setBinaryStream(long pos) throws SQLException {

        return blob;
    }

    public int setBytes(long pos, byte[] bytes, int offset, int len) throws SQLException {

        try {
            byte[] copy = blob.toByteArray();
            System.arraycopy(blob.toByteArray(), 0, copy, 0, len);
            blob.reset();
            blob.write(copy);
            return len;
        } catch (Throwable t) {
            throw new SQLException(t.getMessage());
        }
    }

    public int setBytes(long pos, byte[] bytes) throws SQLException {

        return setBytes(pos, bytes, 0, bytes.length);
    }
}